<?php
// 本类由系统自动生成，仅供测试用途
class ProjectAction extends Action {

	public function add(){
      
		$Data = $_REQUEST["Data"];
		
		$ProjTB = D ( 'Project' );
		$CompanyTB = D ( 'Company' );
		$GroupTB = D ( 'Projectgroup' );
		
		$Proj ['pno'] =$Data ['pno'];
		$Proj ['pname'] =$Data ['pname'];
		$Proj ['state'] = '活跃';
		$Proj ['regdate'] =$Data ['date'];
		
		$lastInsId = $ProjTB->add ( $Proj );
		
		$Company ['project_pid'] = $lastInsId;
		$Company ['name'] =$Data ['cname'];
		$Company ['address'] =$Data ['address'];
		$Company ['classify'] =$Data ['classify'];
		$Company ['contact_name'] =$Data ['cpname'];
		$Company ['contact_position'] =$Data ['cpposition'];
		$Company ['contact_phone'] =$Data ['cpphone'];
		$Company ['contact_email'] =$Data ['cpemail'];
		$Company ['intro'] =$Data ['desc'];
		
		$Group ['project_pid'] = $lastInsId;
		$Group ['source'] =$Data ['source'];
		$Group ['partnername'] =$Data ['partnername'];
		$Group ['managername'] =$Data ['managername'];
		$Group ['membername'] =$Data ['membername'];
		$Group ['prefund'] =$Data ['prefund'];
		
		if ($CompanyTB->add ( $Company ) && $GroupTB->add ( $Group )) {
			session("pid",$lastInsId);		
			session ( "pname", $Data ['pname'] );
			session ( "pno", $Data ['pno'] );		
			session ( "managername", $Data ['managername'] );
			session ( "partnername", $Data ['partnername'] );
			session ( "fund", $Data['fund'] );
			$message="【".session('nickname')."】创建了【".session('pname')."】的 项目基本信息表";
			$this->sendmessage($message);
			print_r("success!");
		} else
			print_r("error!");
		
      
    }
	public function update(){
      
		$Data = $_REQUEST["Data"];
		
		$ProjTB = D ( 'Project' );
		$CompanyTB = D ( 'Company' );
		$GroupTB = D ( 'Projectgroup' );
		
		
		$Proj ['pid'] = session ( 'pid' );
		$Proj ['pno'] =$Data ['pno'];
		$Proj ['pname'] =$Data ['pname'];
		$Proj ['state'] = $Data ['state'];
		$Proj ['regdate'] =$Data ['regdate'];				
		
		$Company ['cid'] = session ( 'cid' );
		$Company ['project_pid'] = session ( 'pid' );
		$Company ['name'] =$Data ['cname'];
		$Company ['address'] =$Data ['address'];
		$Company ['classify'] =$Data ['classify'];
		$Company ['contact_name'] =$Data ['contact_name'];
		$Company ['contact_position'] =$Data ['contact_position'];
		$Company ['contact_phone'] =$Data ['contact_phone'];
		$Company ['contact_email'] =$Data ['contact_email'];
		$Company ['intro'] =$Data ['intro'];
		
		$Group ['gid'] = session ( 'gid' );
		$Group ['project_pid'] = session ( 'pid' );
		$Group ['source'] =$Data ['source'];
		$Group ['partnername'] =$Data ['partnername'];
		$Group ['managername'] =$Data ['managername'];
		$Group ['membername'] =$Data ['membername'];
		$Group ['prefund'] =$Data ['fund'];
		
		if (false !== $ProjTB->save ( $Proj ) && false !== $GroupTB->save ( $Group ) && false !== $CompanyTB->save ( $Company )) {					
			session ( "pname", $Data ['pname'] );
			session ( "pno", $Data ['pno'] );		
			session ( "managername", $Data ['managername'] );
			session ( "partnername", $Data ['partnername'] );
			session ( "membername", $Data ['membername'] );
			session ( "fund", $Data['fund'] );
			session ( "state", $Data ['state'] );
			$message="【".session('nickname')."】更新了【".session('pname')."】的 项目基本信息表";
			$this->sendmessage($message);
			print_r("success!");
		} else
			print_r("error!");
		
		//////////
        
    }
	public function query(){
		$pname=$_REQUEST["pname"];
		$managername=$_REQUEST["managername"];
		$partnername=$_REQUEST["partnername"];
		$fund=$_REQUEST["fund"];
		
		$State=$_REQUEST["state"];
		
		$pageIndex = $_REQUEST["pageIndex"];
		$pageSize = $_REQUEST["pageSize"];
	
		$sortField = $_REQUEST["sortField"];
		$sortOrder = $_REQUEST["sortOrder"];
		
		if(!empty($sortField)){
			if ($sortOrder != "desc") $sortOrder = "asc";
			$order = $sortField . " " . $sortOrder;
		}else{
			$order = "pid desc";
		}
		
		$condition ['pname'] = array ('like', '%' . $pname . '%' );
		$condition ['managername'] = array ('like', '%' . $managername . '%' );
		$condition ['partnername'] = array ('like', '%' . $partnername . '%' );
		$condition ['fund'] = array ('like', '%' . $fund . '%' );
		//$condition ['state'] = array ('like', '%' . $State . '%' ) ;
		
			if($State!='所有')							
				$condition ['state'] = $State;
		
		///
		$Invest=D("Investchange");
		
		$funds=$Invest->field('project_pid,investor')->group('project_pid,investor')->select();
		
		foreach ($funds as $id => $data) {
			if($fundarray[$data['project_pid']])
			$fundarray[$data['project_pid']]=$fundarray[$data['project_pid']].','.$data['investor'];			
			else
			$fundarray[$data['project_pid']]=$data['investor'];			
		}
		
		
		$Dao = D("ProjectView");
		
		$total = $Dao->where ( $condition )->count ();
		
		$list=$Dao->where($condition)->order ( $order )->limit ( $pageIndex * $pageSize. ',' . $pageSize )->select();
		
		$result = array();
		foreach ($list as $id => $data) {
			$data["postfund"]=$fundarray[$data['pid']];
			array_push($result, $data);			
		}
		
		///
		
		$resultData = array("total"=>$total,"data"=>$result);

		$Data=json_encode($resultData);
		print_r($Data);
    
    }
	public function querymeeting(){
		$pname=$_REQUEST["key1"];
		$content=$_REQUEST["key2"];
		
		$pageIndex = $_REQUEST["pageIndex"];
		$pageSize = $_REQUEST["pageSize"];
	
		$sortField = $_REQUEST["sortField"];
		$sortOrder = $_REQUEST["sortOrder"];
		
		if(!empty($sortField)){
			if ($sortOrder != "desc") $sortOrder = "asc";
			$order = $sortField . " " . $sortOrder;
		}else{
			$order = "idmeetinglog desc";
		}
		
		$condition ['pname'] = array ('like', '%' . $pname . '%' );
		$condition ['content'] = array ('like', '%' . $content . '%' ) ;
		
			
		
		
		$Dao=D("Meetinglog");
		
		$total = $Dao->where ( $condition )->count ();
		
		//dump($total);
		$list=$Dao->where($condition)->order ( $order )->limit ( $pageIndex * $pageSize. ',' . $pageSize )->select();
		
		//dump($list);
		$resultData = array("total"=>$total,"data"=>$list);

		$Data=json_encode($resultData);
		print_r($Data);
    
    }
	
	public function addmeeting(){
      
		$Data = $_REQUEST["Data"];
		$Dao=D("Meetinglog");
			
		if($Dao->add($Data))		
		{
			$message="【".session('nickname')."】增加了【".session('pname')."】的 项目例会纪要";
			$this->sendmessage($message);
			print_r("success!");
		}
		else
			print_r("error!");
        
    }
	public function updatemeeting(){
      
		$Data = $_REQUEST["Data"];
		$Dao=D("Meetinglog");
			
		if($Dao->save($Data))		
		{
			$message="【".session('nickname')."】修改了【".session('pname')."】的 项目例会纪要";
			$this->sendmessage($message);
			print_r("success!");
		}
		else
			print_r("error!");
        
    }
	public function select(){
		if(session('pid')){
		$pid=session('pid');
		$Dao=D("ProjectView");
		$condition['pid']=$pid;
		$list=$Dao->where($condition)->find();
		
		$this->assign ( 'list', $list );
		$Data=json_encode($list);
		print_r($Data);
		}
		else {
			$list["error"] = "error";
			$Data = json_encode($list);
			print_r($Data);
		}
		
	}
	public function info(){
	
		$pid=session('pid');
		$Dao=D("ProjectView");
		$condition['pid']=$pid;
		$list=$Dao->where($condition)->find();
		
		if ($list)
		$this->assign ( 'list', $list );
			
		$this->display();
		
		
	
	}
	public function pre_1(){
		
			$Dao = D("infofinance");
			$condition['project_pid'] = session('pid');
			$list = $Dao -> where($condition) -> find();
			
			if ($list)
			{session('idinfo_finance', $list['idinfo_finance']);
			$this->assign ( 'list', $list );}

			$this->display();
		
		
	
	}

	public function toinfo(){
		$Dao = D ( "ProjectView" );
		
		if($_REQUEST['pid'])
		$condition['pid']=$_REQUEST['pid'];
		else if($_REQUEST['pname'])
		$condition['pname']=$_REQUEST['pname'];
		
		$list = $Dao->where ( $condition )->find ();
		$this->assign ( 'list', $list );
		session("pid",$list ['pid']);
		session ( "cid", $list ['cid'] );
		session ( "gid", $list ['gid'] );
		session ( "pname", $list ['pname'] );
		session ( "pno", $list ['pno'] );
		session ( "state", $list ['state'] );
		session ( "managername", $list ['managername'] );
		session ( "partnername", $list ['partnername'] );
		session ( "membername", $list ['membername'] );
		session ( "fund", $list ['fund'] );
		$this->display("info");
	}
	public function export(){
		
		header("Content-type:application/vnd.ms-excel; charset=utf-8");
		header("Content-Disposition:filename=test.xls");
		$columns=json_decode($_REQUEST["columns"]);
		mb_convert_encoding($columns,"gbk", "utf-8");
		print_r($columns);

	}
	public function pre_2(){
		
		session ( 'level', '初访' );
		
		$this->redirect("file");
		
	}
	public function setup(){
		
		session ( 'level', '立项' );
		
		$this->redirect("file");
		
	}
	public function DD(){
		
		session ( 'level', '深入DD' );
		
		$this->redirect("file");
		
	}
	public function invest(){
		
		session ( 'level', '投决' );
		
		$this->redirect("file");
		
	}
	public function post(){
		
		session ( 'level', '投后管理' );
		
		$this->redirect("file");
		
	}
	public function post_7(){
		
		session ( 'level', '投后管理' );
		
		$this->redirect("file");
		
	}
	public function quit_2(){
		
		session ( 'level', '退出' );
		
		$this->redirect("file");
		
	}
	public function file() {
		//session ( 'project', '殷三三' );
		//session ( 'level', '投后管理' );
		$file = D ( "file" );
		
		$project=$_SESSION['pname'];
		$level=$_SESSION ["level"];
		
		if ($level === '初访') {
			$filelist = array ("[项目名称]建档申请表", "[项目名称]商业计划书[YY/MM/DD]", "[项目名称]项目推介报告[YY/MM/DD]", "[项目名称]尽职调查报告（DD主体名称）[YY/MM/DD]", "[项目名称]访谈备忘录（撰写人）[YY/MM/DD]", "[行业名称]行业分析报告（撰写人）[YY/MM/DD]", "[项目名称]财务报表[YY/MM/DD]","[项目名称]保密协议[YY/MM/DD]" );
		} elseif ($level === '立项') {
			$filelist = array ("[项目名称]立项申请表", "[项目名称]立项决议书", "[项目名称]立项报告[YY/MM/DD]", "[项目名称]财务模型[YY/MM/DD]" );
		} elseif ($level === '深入DD') {
			$filelist = array ("[项目名称]尽职调查清单[YY/MM/DD]", "[项目名称]尽职调查报告（DD主体名称）[YY/MM/DD]", "[项目名称] Term Sheet[YY/MM/DD]", "[项目名称]企业关键人尽职调查（撰写人）[YY/MM/DD]", "[项目名称]审计报告[YY/MM/DD]" );
		} elseif ($level === '投决') {
			$filelist = array ("[项目名称]投资决策申请表", "[项目名称]投资决策决议书", "[项目名称]投资建议报告（撰写人）[YY/MM/DD]", "[项目名称]投资协议[YY/MM/DD]", "[项目名称]股东名册[YY/MM/DD]", "[项目名称]公司章程[YY/MM/DD]", "[项目名称]验资报告[YY/MM/DD]", "[项目名称]股东协议[YY/MM/DD]" );
		} elseif ($level === '投后管理') {
			$filelist = array ("[项目名称]投后访谈纪要（撰写人）[YY/MM/DD]", "[项目名称]重大事项备忘录（撰写人）[YY/MM/DD]", "[项目名称]投后季度管理报告（撰写人）[YY/MM/DD]", "[项目名称]（年/季度）财务报表", "[项目名称]年报[YY/MM/DD]", "[项目名称]董事会决议[YY/MM/DD]", "[项目名称]股东会决议[YY/MM/DD]", "[项目名称]营业执照[YY/MM/DD]", "[项目名称]公司章程[YY/MM/DD]","[项目名称]验资报告[YY/MM/DD]" );
		} elseif ($level === '退出') {
			$filelist = array ("[项目名称]股权转让协议[YY/MM/DD]" );
		}
		
		$condition ['project'] = $project;
		$condition ['level'] = $level;
		$list = $file->where ( $condition )->select ();
		$this->assign ( 'list', $list );
		$this->assign ( 'filelist', $filelist );
		$this->assign ( 'level', $level );
		$this->assign ( 'project', $project  );
		$this->display ();
	}
	public function sendmessage($message){
		$Dao=D("Mail");
		$Data['sendname']="系统管理员";
		$Data['getname']=session('managername');
		$Data['senddate']=date('Y-m-d');
		$Data['content']=$message;
		$Data['title']="系统消息";
		
		$Dao->add($Data);		
			
	}
	public function post_2() {
		
			$Dao = D("safeguardclause");
			$condition['project_pid'] = session('pid');
			$list = $Dao -> where($condition) -> find();
			
			if ($list)
			$this->assign ( 'list', $list );
			
			$this->display();
			
	}
}